package cn.pconline.autoclub.web.admin;

import cn.pconline.autoclub.entity.AppConfig;
import cn.pconline.autoclub.service.AppConfigService;
import cn.pconline.autoclub.util.WebUtils;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.gelivable.webmvc.JSONBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author xulin
 */
@RequestMapping(value="/admin")
@Controller
public class AdminAppConfigController {
    @Autowired
    private AppConfigService appConfigService;
    private static final Logger log = LoggerFactory.getLogger(AdminAppConfigController.class);
    private void errorMsg(String msg, HttpServletResponse resp) {
        try {
            resp.setCharacterEncoding("UTF-8");
            resp.getWriter().println(
                    new JSONBuilder()
                    .put("statusCode", 300)
                    .put("message", msg)
                    .toString());
        } catch (IOException ex) {
            log.error("set errorMsg fail!", ex);
        }
    }

     private void successMsg(String msg, String navTabId, HttpServletResponse resp, String callbackType) {
        try {
            String rs = StringUtils.isEmpty(msg) ? "操作成功" : msg;
            resp.setCharacterEncoding("UTF-8");
            resp.getWriter().println(new JSONBuilder()
                    .put("statusCode", 200)
                    .put("message", rs)
                    .put("navTabId", navTabId)
                    .put("callbackType", callbackType)
                    .toString());
        } catch (IOException ex) {
            log.error("set successMsg fail!", ex);
        }
    }
    /**
     * 车友会config配置
     * @param request
     * @param response
     * @return 
     */
    @RequestMapping(value="/config/appConfig.do",method= RequestMethod.GET)
    public String appConfig(HttpServletRequest request,HttpServletResponse response){
        List<AppConfig> list = appConfigService.queryAppConfig(new AppConfig());
        request.setAttribute("appConfigs", list);
        return "admin/system/config";
    }
    /**
     * 创建参数页面
     * @param request
     * @param response
     * @return 
     */
    @RequestMapping(value="/config/create.do",method= RequestMethod.GET)
    public String createConfig(HttpServletRequest request,HttpServletResponse response){
            long configId = WebUtils.paramLong(request, "configId", 0);
            AppConfig appConfig = appConfigService.findAppConfig(configId);
            request.setAttribute("appConfig", appConfig);
        return "admin/system/createConfig";
    }
    /**
     * 创建Config接口
     * @param request
     * @param response 
     */
    @RequestMapping(value="/config/create.do",method= RequestMethod.POST)
    public void create(HttpServletRequest request,HttpServletResponse response){
        WebUtils.setEncoding(request, response);
        String appName = WebUtils.param(request, "appName", "");
        String appKey = WebUtils.param(request, "appKey", "");
        String appValue = WebUtils.param(request, "appValue", "");
         if(StringUtils.isEmpty(appName)){
            errorMsg("配置名称不能为空", response);
            return;
        }
        if(StringUtils.isEmpty(appKey)){
            errorMsg("配置Key不能为空", response);
            return;
        }
        if(StringUtils.isEmpty(appValue)){
            errorMsg("配置Value不能为空", response);
            return;
        }
        if(appConfigService.containValue(appName, appValue)){
            errorMsg("该配置已经存在", response);
            return;
        }
        AppConfig appConfig = new AppConfig();
        appConfig.setAppName(appName);
        appConfig.setAppKey(appKey);
        appConfig.setAppValue(appValue);
        long configId = appConfigService.createAppConfig(appConfig);
        if(configId>0){
            successMsg("创建成功", "list-appConfig", response,"closeCurrent");
        }else{
            errorMsg("创建失败", response);
        }
    }
    /**
     * 修改Config接口
     * @param request
     * @param response 
     */
    @RequestMapping(value="/config/update.do",method= RequestMethod.POST)
    public void update(HttpServletRequest request,HttpServletResponse response){
        WebUtils.setEncoding(request, response);
        long configId = WebUtils.paramLong(request, "configId", 0);
        String appName = WebUtils.param(request, "appName", "");
        String appKey = WebUtils.param(request, "appKey", "");
        String appValue = WebUtils.param(request, "appValue", "");
        if(StringUtils.isEmpty(appKey)){
            errorMsg("配置Key不能为空", response);
            return;
        }
        if(StringUtils.isEmpty(appValue)){
            errorMsg("配置Value不能为空", response);
            return;
        }
        AppConfig appConfig = appConfigService.findAppConfig(configId);
        if(appConfig == null){
            errorMsg("该配置不存在", response);
            return;
        }
        appConfig.setAppName(appName);
        appConfig.setAppKey(appKey);
        appConfig.setAppValue(appValue);
        long id = appConfigService.updateAppConfig(appConfig);
        if(id>0){
            successMsg("创建成功", "list-appConfig", response,"closeCurrent");
        }else{
            errorMsg("创建失败", response);
        }
    }
    /**
     * 删除接口
     * @param request
     * @param resp 
     */
    @RequestMapping(value="/config/delete.do",method= RequestMethod.POST)
    public void delete(HttpServletRequest request,HttpServletResponse resp){
        long configId = WebUtils.paramLong(request, "configId",0);
        AppConfig appConfig = appConfigService.findAppConfig(configId);
        if(appConfig == null){
            errorMsg("该配置不存在", resp);
            return;
        }
        int count = appConfigService.deleteAppConfig(appConfig);
        if(count>0){
            successMsg("删除成功","list-appConfig", resp, null);
            return;
        }else{
            errorMsg("删除失败", resp);
        }
    }
}
